<?php
namespace app\cms\model;
use framework\ext\Category;
/* *
 * 资源特性与专题功能
 * */
class special extends \app\base\model\AdminModel{
    protected $table ='cms_special';  //模型表

    //列表
    public function _select($pid,$website_id,$field = NULL){
        if($website_id){
            $condition['website_id'] = $website_id;
            $condition['pid'] = $pid;
        }else{
            $condition['pid'] = $pid;
        }
        return $this->table('cms_special as A')
            ->join('{pre}website as B ON A.website_id = B.id')
            ->field('A.*,B.title as website_name')
            ->where($condition)
            ->order('a.id desc')
            ->select();
    }

    //单个
    public function _find($condition){
        return $this->where($condition)->find();
    }

    //删除
    public function _delete($fid){
        $subclass = $this->where(array('pid' => $fid))->find(); 
        if($subclass){
            return FALSE;
        }
        $result = $this->where(array('id' => $fid))->delete();
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }  

    //编辑
    public function _update($data,$condition = NULL){
        if($condition){
            return $this->where($condition)->data($data)->update();
        }else{
            return $this->data($data)->insert();
        }
    }  

    //当前路径
    public function path($pid = 0,$url = null) {
        $condition['id'] = intval($pid);
         $result = $this->where($condition)->field('id,pid,name')->find();
        if ($result) {
            $Str = $this->path($result['pid'],$url);
            if($url){
                $Str .= ' / <a href="' . url(''.$url.'',array(id=>$result['id'])).'">' . $result['name'] . '</a>';
            }else{
                $Str .= ' / <a href="###">' . $result['name'] . '</a>';
            }
        }
        return $Str;
    }

    //获取分类树，$id，分类id,$id=0，获取所有分类结构树
    public function getClass($id=0) {
        $data = $this->select('','id,pid,name'); //第一种 
        $cat = new Category(array('id', 'pid', 'name', 'cname'));
        return $cat->getTree($data, $id);
    }
    /* *
    *  资源特性下拉菜单
    *  $rootid:站点跟ID,
    *  $pid：上级ID
    *  $channel_id：当前ID
    *  $select_id :当前选择的ID
    * */
    public function CreateSpecialTree($rootid,$channel_id,$select_id = NULL,$pid = 0){
        //查询栏目站点
        $rootid = intval($rootid);
        $channel_id = intval($channel_id);
        $str_checked = $select_id?explode(',',$select_id):array();
        if($rootid){
            $website = model('channel')->_find(array('id'=>$rootid),'website_id');
            $condition['website_id'] = $website['website_id'];   
        }
        $condition['pid'] = intval($pid);
        $condition['0'] = '(channel_id = 0 or channel_id = '.$channel_id.')';
        $rel = $this->where($condition)->field('id,name')->order('id desc')->select(); 
        if($rel){
            if($pid)echo '<ul>';
            foreach($rel as $rs){
                if(is_array($str_checked)){
                    if(in_array($rs['id'], $str_checked)){
                        $checked = 'checked="checked" ';
                    }
                }
                echo '<li><input type="checkbox" name="special[]" value="'.$rs['id'].'" '.$checked.'/> '.$rs['name'];
                $this->CreateSpecialTree(0,$channel_id,$select_id,$rs['id']);
                echo '</li>';
            }
            if($pid)echo '</ul>';
        }else{
            if(!$pid) echo '<li><input type="checkbox" name="special[]" value="" disabled="true"> 未找到资源特性</li>';
        }     
    }
}